/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package vista;

import java.util.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import modelo.IEmpleado;
/**
 *
 * @author LEONIC
 */
public class EmpleadoTableModel implements TableModel{

    private List<IEmpleado> empleados = new ArrayList<IEmpleado>();
    private List<TableModelListener> listeners = new ArrayList<TableModelListener>();

    public List<IEmpleado> getEmpleados() {
        return empleados;
    }

    public void setEmpleados(List<IEmpleado> empleados) {
        this.empleados = empleados;
        fireContentsChanged();
    }

    public IEmpleado getRow(int indexRow){
        return empleados.get(indexRow);
    }
    public int getRowCount() {
        return empleados.size();
    }

    public int getColumnCount() {
        return 10; //ya que tendremos tres valores a mostrar por contacto
    }

    public String getColumnName(int columnIndex) {
        String name = null;
        switch (columnIndex) {
            case 0:
                name = "dni";
                break;
            case 1:
                name = "nombre";
                break;
            case 2:
                name = "salario";
                break;
            case 3:
                name = "tarea";
                break;
            case 4:
                name = "rol";
                break;
            case 5:
                name = "empleado";
                break;
            case 6:
                name = "cod_tienda";
                break;
            case 7:
                name = "usuario";
                break;
            case 8:
                name = "password";
                break;
            case 9:
                name = "pluses";
                break;
        }
        return name;
    }

    public Class<?> getColumnClass(int columnIndex) {
        return String.class; //Todos los valores de contacto son un String
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false; //No permito editar celdas
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        IEmpleado prov = empleados.get(rowIndex);
        String value = null;
        switch (columnIndex) {
            case 0:
                value = prov.getDNI();
                break;
            case 1:
                value = prov.getNombre();
                break;
            case 2:
                value = Double.toString(prov.getSalario());
                break;
            case 3:
                value = prov.getTarea();
                break;
            case 4:
                value = prov.getRol();
                break;
            case 5:
                value = Boolean.toString(prov.getEmpleado());
                break;
            case 6:
                value = Integer.toString(prov.getCodigoTienda());
                break;
            case 7:
                value = prov.getUsuario();
                break;
            case 8:
                value = prov.getPassword();
                break;
            case 9:
                value = Double.toString(prov.getPluses());
                break;
        }
        return value;

    }

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void addTableModelListener(TableModelListener l) {
        listeners.add(l);
    }

    public void removeTableModelListener(TableModelListener l) {
        listeners.remove(l);
    }

     protected void fireContentsChanged() {
        //TableModelEvent event = new TableModelEvent(this,0,getRowCount());
         TableModelEvent event = new TableModelEvent(this, 0, this.getRowCount()-1, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT);
        for (TableModelListener listener : listeners) {
            listener.tableChanged(event);
        }
    }
}
